coding: "utf-8"
import json
import requests
import random
import pymysql
import os
import sys
sys.path.append(os.getcwd())  #当前根目录加到环境变量中

from week10.lesson10_1.jd_crawler.jd_parser.search import search_item
from week10.lesson10_1.jd_crawler.settings import MYSQL_LOCAL_CONF


#search 的headers
search_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
"Upgrade-Insecure-Requests":"1",
"Cookie": "__jdu=1581938703; shshshfpa=50968fe4-bc57-87e5-9309-816cfd5f5afb-1606399652; shshshfpb=liFYCwNJx3li09yCFyzzV6g%3D%3D; qrsc=3; areaId=13; ipLoc-djd=13-1007-4909-0; PCSYCityID=CN_370000_370200_370213; 3AB9D23F7A4B3C9B=FJHKSFW65CKLRZUQZPVFPDE236S3YB2LHYFSINAXWIOA3U63PGUZURO3KHX47EJQR5TIHXDGHJNUUI36K7HO2ILRWU; unpl=V2_ZzNtbUdfE0JzDUBRLhkMB2IEG10SVBNGdQxEViweCABnVkBdclRCFnQURldnGVUUZgQZXkVcRxZFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHsdVQVvARtbQ1VzJXI4dmR8HVUAYQEiXHJWc1chVEFXeh1eAyoDFlRCX0EccwlEZHopXw%3d%3d; CCC_SE=ADC_7kDob6OXvhF69C4MtTcxGU7SuYJkUKvhTlzhuJMoV7GvW5WHaCBZ8l8N8iM%2fBzDd09fK1Wwb5cnkoroOE8Z%2fXjHhzgSjL%2fECI0gOcTY7tcVe9GDr9kdvqitl%2bFVl05mZbtb2WUQf2W%2fXO0KOZzEpTbqHTo4fV6BYsmaXE%2blxMa2wlNk%2f57AE82oL3KG40MkcQnCPEudrcI5%2bsIaLeNX39gkPBybdo%2fa2hCjv9nVDEXAjOWndhJc5XBBYMMPCmxDwcYznN%2bFqBjupUnDBLS9s6%2fvmJ%2fGYULcOipA8OLpI8Ma3SPItSsOt%2bfLTpRHxsM0hOHgdRRmjVTuZVxHDOHoRVEq6CKJfzroZIJbQFattpEBEL1NWozfckExHYqo8PYH9V7v%2fVm4SIK0SB3mPSIlMVL8TcsI0yBuZBjl1Egr3sC1EcDHN3PTW8LsHUTqtbNMhZm9fUCRt6Y9iA6qAixyUkbxtEkgAVQFYlRJbo4wIUovKPFmp%2bcKXSwwJylIkz%2bVtwF7GavQzJDO75WjK%2be0%2fufQdgljXR3ouk5lKGj5cAYS1NYyUUrMpP3wI9ucaTDuEb4ZfO%2bMdduEJIZIEDCWNyw%3d%3d; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_49af7474d1a34681a2ab1533f6e41dc1|1608244014670; __jda=122270672.1581938703.1606399641.1607865537.1608244015.8; __jdc=122270672; shshshfp=f61d3301bf123364b03b4f78537d11e0; rkv=1.0; __jdb=122270672.6.1581938703|8.1608244015; shshshsID=69430882eacd0e8e0fd3d4c3802b49b0_5_1608244454367",
          }


#search存储器
def search_save_item(item):
    #search表格
    SQL_search = "INSERT INTO jd_search(img, price, name, shop, icons, sta_date)VALUES (%s,%s,%s,%s,%s,%s)"
    #search执行
    cursor.executemany(SQL_search, item)
    mysql_server.commit()


#search下载器
def request_search(keyword):
    url = "https://search.jd.com/Search"
    params = {
        "keyword": keyword,
        "wq": keyword,
        "psort": 3,
        "click": 0
    }
    proxy = random.choice(ip_arry) #随机取代理的一个IP
    proxies = {
        "http":f"http://{proxy['ip']}:{proxy['port']}",
    }
    response = requests.get(url=url, params=params, headers=search_headers, proxies = proxies)
    return response.text


def main():   #主函数
    #search
    for keyword in keyword_arry:
        result_search = request_search(keyword)
        item_arry = search_item(result_search)
        search_save_item(item_arry)
    cursor.close()
    mysql_server.close()
    print("success!")


if __name__ == "__main__":
    mysql_server = pymysql.connect(**MYSQL_LOCAL_CONF)
    cursor = mysql_server.cursor()
    keyword_array = ["键盘", "鼠标", "显示器"] #代替生产者
    json_data = json.loads('''{"code":0,"data":[{"ip":"27.206.77.188","port":4245},{"ip":"140.255.45.219","port":4258},{"ip":"122.4.45.224","port":4242},{"ip":"123.133.201.125","port":4278},{"ip":"27.206.180.162","port":4245},{"ip":"60.213.88.22","port":4220},{"ip":"182.34.203.223","port":4246},{"ip":"112.248.8.248","port":4282},{"ip":"113.128.9.117","port":4264},{"ip":"112.251.28.60","port":4278},{"ip":"113.124.92.41","port":4234},{"ip":"122.4.41.202","port":4242},{"ip":"27.220.125.222","port":4245},{"ip":"122.4.50.165","port":4242},{"ip":"222.132.197.180","port":4220},{"ip":"113.121.21.140","port":4234},{"ip":"113.124.92.138","port":4234},{"ip":"113.120.61.48","port":4264},{"ip":"123.133.199.122","port":4245},{"ip":"182.34.209.27","port":4246}],"msg":"0","success":true}''')
    ip_arry = json_data["data"]
    main()